package com.jiejie.controller;


import com.jiejie.entity.Comments;
import com.jiejie.entity.LoginCustomer;
import com.jiejie.service.ICommentsService;
import com.jiejie.util.TokenUtil;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 辰广
 * @since 2022-10-05
 */
@RestController
@RequestMapping("comments")
public class CommentsController {
    @Resource
    private ICommentsService commentsService;
    @PostMapping("/saveComment")
    public String saveComments(@RequestParam(value = "commentText") String commentText,
                               @RequestParam(value = "movieId") Integer movieId,
                               HttpServletRequest request,
                               HttpServletResponse response){
    String token=request.getHeader("token");
    System.out.println(token);
    System.out.println(commentText);
    System.out.println(movieId);
    LoginCustomer loginCustomer=null;
    if(!token.equals("null")){
        loginCustomer= TokenUtil.parseToken(token);
    }
    System.out.println(commentText.length());
    if(commentText.length()==0){
        return "评论失败";
    }
    Comments comments=new Comments();
    comments.setCommentsContent(commentText);
    comments.setCommentsLikes(0);
    comments.setCustomerId(loginCustomer.getCustomerId());
    comments.setCommentsStatus(0);
    comments.setCommentsCreateTime(LocalDateTime.now());
    comments.setCommentsUpdateTime(LocalDateTime.now());
    comments.setMovieId(movieId);
    comments.setCommentsVersion(0);
    comments.setCommentsScore("0");
    commentsService.saveComment(comments);
    return "评论成功";
}
}
